<?php
    //require section
    require_once dirname(__FILE__) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_security.php';
    $title = 'Đăng Nhập';

?>

<!DOCTYPE html>
<html>
<head>
    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php';
    ?>
</head>
<body>
<?php

    if (!empty($_POST)) {
        $txt_email    = $_POST['txt_email'];
        $txt_password = $_POST['txt_password'];
        $result       = api_security::login($txt_email, $txt_password);
        if ($result['user_login'] === 'success') {
            session_start();

            $_SESSION['user_login'] = 'logined';
            if ($result['user_type'] == 1) {
                $_SESSION['user'] = 'Admin';
                lib_redirect::Redirect('/admin/index.php');
            } else {
                $_SESSION['user'] = $result['email'];
                $_SESSION['user_info'] = $result;
                lib_redirect::Redirect('/index.php');
            }

        } else {
            $error = "Đăng nhập thất bại.";
        }
    }
?>
<div class="uk-container uk-container-center uk-margin-top">
    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'menu.php';
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'slider.php';
    ?>

    <!-- Changable content -->
    <section id="main-content" class="ts-body-container uk-panel uk-panel-box ts-padding-large">
        <article class="uk-article">
            <h1 class="uk-article-title ts-color-primary">Đăng Nhập</h1>
            <hr class="uk-article-divider">
            <div>
                <form name="form_data" id="form_data" class="uk-form" method="POST" enctype="multipart/form-data"
                      action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

                    <!-- Hidden Value Section -->

                    <!-- End Hidden Value Section -->
                    <div class="error">
                        <?php
                            if (!empty($error)) {
                                echo $error;
                            }
                        ?>
                    </div>
                    <table class="ts-table">
                        <tr>
                            <th><label for="txt_email">Email:<span class="ts-alert">*</span></label></th>
                            <td><input name="txt_email" class="uk-form-width-large" type="text" placeholder="Email"
                                       required></td>
                        </tr>
                        <tr>
                            <th><label for="txt_password">Mật khẩu:<span class="ts-alert">*</span></label></th>
                            <td><input name="txt_password" class="uk-form-width-large" type="password"
                                       placeholder="Mật khẩu" required></td>
                        </tr>
                        <tr>
                            <th></th>
                            <td><input type="submit" name="btn_submit" value="Đăng Nhập"
                                       class="uk-button uk-button-large uk-button-primary"></td>
                        </tr>
                    </table>
                </form>
            </div>
        </article>
    </section>
    <!-- End changable content -->

    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
    ?>
</div>
</body>
</html>